<template>
  <div class="com-container">
    <div class="com-chart" ref="rank_ref"></div>
  </div>
</template>
<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      allData: null, //存储请求过来的数据
      echartInstance: null, //服务器获取过来的数据
      fontSize: 0,
      startValue: 0, //数据窗口范围的起始数值
      endValue: 9, //数据窗口范围的结束数值
      timeId: null //定时器标识
    };
  },
  created() {
    this.$socket.registerCallBack("rankData", this.getData);
  },
  mounted() {
    this.initChart();
    // this.getData();
    // 发送数据
    this.$socket.send({
      action: "getData",
      socketType: "rankData",
      chartName: "rank",
      value: ""
    });
    window.addEventListener("resize", this.screenAdapter);
    // 在刚进入页面时，主动进行屏幕适配
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timeId);
    this.$socket.unRegisterCallBack("rankData");
  },
  methods: {
    initChart() {
      this.echartInstance = this.$echarts.init(this.$refs.rank_ref, this.theme);
      this.echartInstance.on("mouseover", () => {
        clearInterval(this.timeId);
      });
      this.echartInstance.on("mouseout", () => {
        this.startInterval();
      });
      const initOption = {
        title: {
          text: "▎地区销售排行",
          left: 20,
          top: 20
        },
        grid: {
          top: "40%",
          left: "5%",
          right: "5%",
          bottom: "5%",
          containLabel: true
        },
        tooltip: {
          show: true
        },
        xAxis: {
          type: "category"
        },
        yAxis: {
          type: "value"
        },
        series: [
          {
            type: "bar"
          }
        ]
      };
      this.echartInstance.setOption(initOption);
    },
    getData(ret) {
      // const { data: ret } = await this.$http.get("rank");
      this.allData = ret;
      // 对数据排序
      this.allData.sort((a, b) => {
        return b.value - a.value; //从大到小排序
      });
      this.updateChart();
      this.startInterval();
    },
    updateChart() {
      const provinceArr = this.allData.map(item => {
        return item.name;
      });
      const valueArr = this.allData.map(item => {
        return item.value;
      });
      const dataOption = {
        xAxis: {
          data: provinceArr
        },
        dataZoom: [
          {
            show: false,
            startValue: this.startValue,
            endValue: this.endValue
          }
        ],
        series: [
          {
            data: valueArr,
            itemStyle: {
              color: arg => {
                // console.log(arg);
                if (arg.value > 300) {
                  return "blue";
                } else if (arg.value > 200) {
                  return "skyblue";
                } else {
                  return "cyan";
                }
              }
            }
          }
        ]
      };
      this.echartInstance.setOption(dataOption);
    },
    // 屏幕适配
    screenAdapter() {
      this.fontSize = (this.$refs.rank_ref.offsetWidth / 100) * 3.6;
      // 分辨率相关配置相关
      const adapterOption = {
        title: {
          // 设置标题样式
          textStyle: {
            fontSize: this.fontSize
          }
        },
        series: [
          {
            barWidth: this.fontSize,
            itemStyle: {
              barBorderRadius: [this.fontSize / 2, this.fontSize / 2, 0, 0]
            }
          }
        ]
      };
      this.echartInstance.setOption(adapterOption);
      this.echartInstance.resize();
    },
    // 设置定时器
    startInterval() {
      if (this.timeId) clearInterval(this.timeId);
      this.timeId = setInterval(() => {
        this.startValue++;
        this.endValue++;
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0;
          this.endValue = 9;
        }
        this.updateChart();
      }, 2000);
    }
  },
  computed: {
    ...mapState(["theme"])
  },
  watch: {
    theme() {
      // 销毁当前图表
      this.echartInstance.dispose();
      // 重新创建图表
      this.initChart();
      // 完成屏幕适配
      this.screenAdapter();
      // 更新图表的展示
      this.updateChart();
    }
  }
};
</script>
<style scoped lang='less'>
</style>